package com.codedancer.campus.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.client.service.IContactAdminService;
import com.codedancer.campus.client.service.bo.contactAdmin.ContactAdminChatMessagePageSearchBo;
import com.codedancer.campus.client.service.vo.contactAdmin.ContactAdminChatRoomVo;
import com.codedancer.campus.client.service.vo.contactAdmin.ContactAdminChatMessagePageVo;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.mapper.ContactAdminChatMessageMapper;
import com.codedancer.campus.mapper.ContactAdminChatRoomMapper;
import com.codedancer.campus.po.ContactAdminChatMessage;
import com.codedancer.campus.po.ContactAdminChatRoom;
import com.codedancer.campus.qo.contactAdmin.ContactAdminChatMessagePageSearchQo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContactAdminServiceImpl implements IContactAdminService {

    @Autowired
    private ContactAdminChatMessageMapper contactAdminChatMessageMapper;
    @Autowired
    private ContactAdminChatRoomMapper contactAdminChatRoomMapper;

    /**
     * 获取聊天记录
     *
     * @param contactAdminChatMessagePageSearchBo
     * @return
     */
    @Override
    public IPage<ContactAdminChatMessagePageVo> pageHistory(ContactAdminChatMessagePageSearchBo contactAdminChatMessagePageSearchBo) {
        ContactAdminChatMessagePageSearchQo contactAdminChatMessagePageSearchQo = BeanUtil.copyProperties(contactAdminChatMessagePageSearchBo, ContactAdminChatMessagePageSearchQo.class);
        IPage<ContactAdminChatMessage> contactAdminChatMessageIPage = contactAdminChatMessageMapper.pageHistory(new Page<ContactAdminChatMessage>(contactAdminChatMessagePageSearchBo.getPage(), contactAdminChatMessagePageSearchBo.getSize()), contactAdminChatMessagePageSearchQo);
        List<ContactAdminChatMessagePageVo> contactAdminChatMessagePageVos;
        IPage<ContactAdminChatMessagePageVo> contactAdminChetMessagePageVoIPage = new Page<>();
        contactAdminChatMessagePageVos = contactAdminChatMessageIPage.getRecords().stream().map(contactAdminChatMessage -> {
            ContactAdminChatMessagePageVo contactAdminChatMessagePageVo = BeanUtil.copyProperties(contactAdminChatMessage, ContactAdminChatMessagePageVo.class);
            contactAdminChatMessagePageVo.setSendTime(contactAdminChatMessage.getCreateTime());
            return contactAdminChatMessagePageVo;
        }).collect(Collectors.toList());
        contactAdminChetMessagePageVoIPage.setRecords(contactAdminChatMessagePageVos);
        contactAdminChetMessagePageVoIPage.setTotal(contactAdminChatMessageIPage.getTotal());
        return contactAdminChetMessagePageVoIPage;
    }

    /**
     * 获取和某个管理员的聊天室
     *
     * @param currentLoginUuid
     * @param adminUuid
     * @return
     */
    @Override
    public ContactAdminChatRoomVo getChatRoom(String currentLoginUuid, String adminUuid) {
        ContactAdminChatRoom contactAdminChatRoom = contactAdminChatRoomMapper.getChatRoom(currentLoginUuid, adminUuid);
        if (ObjectUtil.isEmpty(contactAdminChatRoom)) {
            // 生成聊天室
            contactAdminChatRoom = ContactAdminChatRoom.builder()
                .uuid(UUIDGenerator.getUUID())
                .userUuid(currentLoginUuid)
                .adminUuid(adminUuid).build();
            contactAdminChatRoomMapper.insert(contactAdminChatRoom);
        }
        return BeanUtil.copyProperties(contactAdminChatRoom, ContactAdminChatRoomVo.class);
    }
}
